import java.util.Random;
import java.util.concurrent.Callable;

public class demo1 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        return null;
    }

    static class num implements Runnable{
        static int max = 20;
        static int number = 20;

        public static synchronized void make() {
            //这个while避免了虚假唤醒的问题
                while (number == max) {
                    try {
                        Thread.currentThread().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            System.out.println("线程"+Thread.currentThread().getName()+"正在生产存量");
        }

        public synchronized  static void eat() {
                while (number == 0) {
                    try {
                        Thread.currentThread().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            System.out.println("线程"+Thread.currentThread().getName()+"正在消化存量");
        }

        @Override
        public void run() {
            Random random = new Random(2);
            int i = random.nextInt();
            if (i == 0) {
                make();
            } else {
                eat();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        num num = new num();
        for (int i = 0; i <10 ; i++) {
            Thread thread = new Thread(num);
            thread.start();
        }
    }
}
